In [1]:
type(())


Out[1]:
tuple

In [2]:
", ".join(dir(()))


Out[2]:
'__add__, __class__, __contains__, __delattr__, __dir__, __doc__, __eq__, __format__, __ge__, __getattribute__, __getitem__, __getnewargs__, __gt__, __hash__, __init__, __init_subclass__, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __rmul__, __setattr__, __sizeof__, __str__, __subclasshook__, count, index'

In [3]:
t = tuple()
print(t)


()

In [4]:
string = "Hello o/"
lst = [1, 2, 3, 4.2, "this", "is", "a", "test", 4j+2]
t = (23.0, 42.0, "here")

print(tuple(string))
print(tuple(lst))
print(tuple(t))


('H', 'e', 'l', 'l', 'o', ' ', 'o', '/')
(1, 2, 3, 4.2, 'this', 'is', 'a', 'test', (2+4j))
(23.0, 42.0, 'here')

In [5]:
control = (2)
single1 = (2,) # <--- the `,` makes the difference
single2 = 2,

print(type(control), type(single1), type(single2))


<class 'int'> <class 'tuple'> <class 'tuple'>

In [6]:
point = (1.2, 3.4, 5.6, 9.8)
# or 
#point = 1.2, 3.4, 5.6, 9.8

In [7]:
point.count(3.4)


Out[7]:
1

In [8]:
point.count(0)


Out[8]:
0

In [9]:
point.index(5.6)


Out[9]:
2

In [10]:
#point.index(0)

#---------------------------------------------------------------------------
#ValueError                                Traceback (most recent call last)
#<ipython-input-7-50c1b2cd61a9> in <module>()
#----> 1 point.index(0)
#
#ValueError: tuple.index(x): x not in tuple

In [11]:
0 in point


Out[11]:
False

In [12]:
point


Out[12]:
(1.2, 3.4, 5.6, 9.8)

In [13]:
point[2]


Out[13]:
5.6

In [14]:
point[1:3]


Out[14]:
(3.4, 5.6)

In [15]:
for _ in point:
    print(_)


1.2
3.4
5.6
9.8

In [16]:
#point[0] = 2.0

#---------------------------------------------------------------------------
#TypeError                                 Traceback (most recent call last)
#<ipython-input-13-8ce6d93959b6> in <module>()
#----> 1 point[0] = 2.0
#
#TypeError: 'tuple' object does not support item assignment

In [17]:
print(point)

t = (2.0, ) + point[1:]

print(t)


(1.2, 3.4, 5.6, 9.8)
(2.0, 3.4, 5.6, 9.8)

In [18]:
list(point)


Out[18]:
[1.2, 3.4, 5.6, 9.8]

In [19]:
another_point = (1, 2, 3, 4)

In [20]:
point + another_point


Out[20]:
(1.2, 3.4, 5.6, 9.8, 1, 2, 3, 4)

In [21]:
point * 2


Out[21]:
(1.2, 3.4, 5.6, 9.8, 1.2, 3.4, 5.6, 9.8)

In [22]:
s = point
id(s) == id(point)


Out[22]:
True

In [23]:
a = tuple([0, 1, 2])
b = tuple([0, 3, 4])
print(a < b) # because 1 < 3

a = (0, 1, 200000000)
print(a < b) # because 1 < 3


True
True

In [24]:
a = 23
b = 42

print(a, b)

a, b = b, a

print(a, b)


23 42
42 23

In [25]:
addr = 'monty@python.org'
uname, domain = addr.split('@')

print(uname)
print(domain)


monty
python.org

In [26]:
def min_max(values):
    return min(values), max(values)

first, last = min_max(list(range(100)))
print(first, last)


0 99

In [27]:
def print_all(*args): # gather
    print(args)
    
print_all(1, 2.0, '3')


(1, 2.0, '3')

In [28]:
t = (7, 3)
#print(divmod(t))
#---------------------------------------------------------------------------
#TypeError                                 Traceback (most recent call last)
#<ipython-input-32-c3fe26201aaf> in <module>()
#      1 t = (7, 3)
#----> 2 print(divmod(t))
#
#TypeError: divmod expected 2 arguments, got 1

print(divmod(*t)) # scatter


(2, 1)

In [29]:
s = 'abc'
t = [1, 2, 3]

In [30]:
for pair in zip(s, t):
    print(pair)


('a', 1)
('b', 2)
('c', 3)

In [31]:
for letter, number in zip(s, t):
    print(letter, number)


a 1
b 2
c 3

In [32]:
for index, element in enumerate('abc'):
    print(index, element)


0 a
1 b
2 c

In [33]:
list(zip(s, t))


Out[33]:
[('a', 1), ('b', 2), ('c', 3)]

In [34]:
list(enumerate('abc'))


Out[34]:
[(0, 'a'), (1, 'b'), (2, 'c')]

In [35]:
list(zip('Anne', 'Elk'))


Out[35]:
[('A', 'E'), ('n', 'l'), ('n', 'k')]

In [36]:
d = {'a':0, 'b':1, 'c':2}

for key, value in d.items():
    print(key, value)


a 0
b 1
c 2

In [37]:
t = [('a', 0), ('c', 2), ('b', 1)]
d = dict(t)
print(d)


{'a': 0, 'c': 2, 'b': 1}

In [38]:
d = dict(zip('abc', range(3)))
print(d)


{'a': 0, 'b': 1, 'c': 2}

Named Tuple


In [39]:
from collections import namedtuple

Car = namedtuple('Car', 'color mileage')
my_car = Car('red', 3812.4)

print(my_car.color)
print(my_car.mileage)
print(my_car)


red
3812.4
Car(color='red', mileage=3812.4)

In [40]:
try:
    my_car.color = 'blue'
except AttributeError as _:
    print(_)


can't set attribute